package com.han.hotelplat.comm;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.han.hotelplat.comm.utils.LogUtil;
import com.han.hotelplat.comm.utils.ValidateUtil;

/**
 * 	用于动�?读取jdbc配置
 * 		jdbc.local.properties 本地数据�?
 * 		jdbc.produce.properties 正式服数据库
 * 		jdbc.special.properties 特殊配置数据库[在Linux环境下，若配置了此文件会覆盖jdbc.produce.properties]
 * @author Administrator
 *
 */
public class JdbcConfigurer extends PropertyPlaceholderConfigurer {
	
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
		String testing = "jdbc.special.properties";//测试环境
		String produce = "jdbc.produce.properties";//生产环境
		String local = "jdbc.local.properties";//本地
		
		InputStream is = null;
		Properties jdbcConfig = new Properties();
		boolean isSpecialEnviroment = false;
		try {
			String OS = System.getProperty("os.name");
			if(!ValidateUtil.str_isEmpty(OS)){
				if(OS.toLowerCase().indexOf("linux") > -1){//linux  分为正式服，测试�?
					is =  JdbcConfigurer.class.getClassLoader().getResourceAsStream(testing);//读取jdbc.testing.properties
					jdbcConfig.load(is);//载入配置
					if(jdbcConfig.stringPropertyNames().size() == 0){//默认正式�?
						System.out.println(String.format("System is loading config file : [%s]", produce));
						is =  JdbcConfigurer.class.getClassLoader().getResourceAsStream(produce);//jdbc
					} else {//特殊配置为测试服
						isSpecialEnviroment = true;
					}
				} else {//本地环境
					System.out.println(String.format("System is loading config file : [%s]", local));
					is =  JdbcConfigurer.class.getClassLoader().getResourceAsStream(local);//jdbc
				}
			} else {
				System.err.println(String.format("System.getProperty('os.name') is null. System is loading config file : [jdbc.produce.properties]"));
				is =  JdbcConfigurer.class.getClassLoader().getResourceAsStream(produce);//jdbc
			}
			if(!isSpecialEnviroment){
				jdbcConfig.load(is);
			}
			is.close();
			Set<String> entrys = jdbcConfig.stringPropertyNames();
			for (String key : entrys) {
				props.setProperty(key, jdbcConfig.getProperty(key));
			}
			System.out.println(String.format("正在准备连接到数据库%s", jdbcConfig.getProperty("master.jdbc.url")));
			String url = jdbcConfig.getProperty("master.jdbc.url");
			if(url.contains("hotel_180812") || url.contains("hotel_dev")){//测试环境
				Constant.CashExtract = false;//不开放提现
			}
			LogUtil.info("正在准备连接到数据库"+ jdbcConfig.getProperty("master.jdbc.url"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		super.processProperties(beanFactoryToProcess, props);
	}

}
